//
//  DXHomeViewController.swift
//  新浪微博
//
//  Created by dix on 2017/1/16.
//  Copyright © 2017年 dix. All rights reserved.
//

import UIKit
// cell 可重用标识符
private let homeCellId = "homeCellId"
class DXHomeViewController: DXVisitorViewController {

    lazy var homeViewModel: DXHomeViewModel = DXHomeViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
            visitorView?.setUpVisiorViewInfo(imgName: nil, title: nil)
            return
        }
        setupUI()
        loadData()
    }
    //MARK: - 设置视图
   private func setupUI(){
        setupNAV()
        setUpTableVIewInfo()
    }
    //MARK: - 设置导航
   private func setupNAV(){
    //左侧
    navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_friendsearch", targrt: self, action: #selector(leftClick))
    navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: "navigationbar_pop", targrt: self, action: #selector(rightClick))
    
    }
    //设置tableView相关信息
    private func setUpTableVIewInfo(){
        //设置数据源
        tableView.dataSource = self
        tableView.delegate = self
        //注册
        tableView.register(DXHomeCell.self, forCellReuseIdentifier: homeCellId)
        //设置行高
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        tableView.separatorStyle = .none
        //设置footView
        tableView.tableFooterView = footView
    }
    //懒加载控件
    fileprivate lazy var footView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        //设置颜色
        view.color = DXThemeColor
        return view
    }()
}
extension DXHomeViewController: UITableViewDataSource, UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.dataArr.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCellId, for: indexPath) as! DXHomeCell
        cell.statusViewModel = homeViewModel.dataArr[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // 最后一行的时候才可以风火轮的动画 而且风火轮没有开启动画
        if indexPath.row == homeViewModel.dataArr.count - 1 && !footView.isAnimating {
            footView.startAnimating()
        }
    }
}
extension DXHomeViewController{
    
    //请求首页数据
    func loadData(){
        
        //使用homeViewModel 请求数据
        homeViewModel.getHomeData { (isFinish) in
            //如果失败了
            if !isFinish {
                print("请求失败")
                return
            }else {
                //刷新UI
                self.tableView.reloadData()
            }
        }
    }
}
extension DXHomeViewController{
   @objc fileprivate func leftClick(){
        print("左侧按钮点击")
    }
   @objc fileprivate func rightClick(){
        let tempVC = DXTempViewController()
        navigationController?.pushViewController(tempVC, animated: true)
    }
    
}
